const log4js = require('log4js')

const levels = {
  'trace': log4js.levels.TRACE,
  'debug': log4js.levels.DEBUG,
  'info': log4js.levels.INFO,
  'warn': log4js.levels.WARN,
  'error': log4js.levels.ERROR,
  'fatal': log4js.levels.FATAL,
}

log4js.configure({
  // 装载器
  appenders: {
    console: { type: 'console' },
    info: {
      type: 'file',
      filename: 'logs/application.log'
    },
    debug: {
      type: 'file',
      filename: 'logs/application.log'
    },
    // 仅保存 1 天
    error: {
      type: 'dateFile',
      filename: 'logs/error',
      pattern: 'yyyy-MM-dd.log',
      alwaysIncludePattern: true, // 文件名称是 filename + pattern
    }
  },
  // log 配置
  categories: {
    default: {
      appenders: ['console'],
      level: 'debug'
    },
    info: {
      appenders: ['info', 'console'],
      level: 'info'
    },
    debug: {
      appenders: ['console', 'debug'],
      level: 'debug'
    },
    error: {
      appenders: ['console', 'error'],
      level: 'error'
    }
  }
})

/*
  debug 日志
*/
const log = {}
log.debug = content => {
  let logger = log4js.getLogger('debug');
  logger.level = levels.debug;
  logger.debug(content);
}

log.error = content => {
  let logger = log4js.getLogger('error');
  logger.level = levels.error;
  logger.error(content);
}

log.info = content => {
  console.log('info', content);
  let logger = log4js.getLogger('info');
  logger.level = levels.info;
  logger.info(content);
}

module.exports = log
